﻿using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Json;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinForm_Mes.Common.Models.WarehouseArea;
using WinForm_Mes.Common.PublicFields;
using static WinForm_Mes.WarehouseManagement.WarehouseAreaForm;

namespace WinForm_Mes.WarehouseManagement
{
    public partial class WarehouseAreaAddForm : DevExpress.XtraEditors.XtraForm
    {
        public delegate void WarehouseAddedHandler();
        public event WarehouseAddedHandler WarehouseAdded;

        public WarehouseAreaAddForm(string newOrderNumber, List<ComboBoxItemWarehouseArea> data)
        {
            InitializeComponent();
            lookUpEdit1.Properties.DataSource = data;
            lookUpEdit1.Properties.DisplayMember = "WarehouseName";
            lookUpEdit1.Properties.ValueMember = "Id";
            lookUpEdit1.Properties.Columns.Clear();
            lookUpEdit1.Properties.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("WarehouseName", "仓库名称"));
            lookUpEdit1.Properties.NullText = "";
            lookUpEdit1.Refresh();
            labelControl8.Text = newOrderNumber; // 绑定新仓库区域编号
        }

        private void WarehouseAreaAddForm_Load(object sender, EventArgs e)
        {

        }

        private async void simpleButton1_Click(object sender, EventArgs e)
        {
            WarehouseAreaModel model = new WarehouseAreaModel
            {
                WarehouseAreaCode = labelControl8.Text.Trim(),
                WarehouseAreaName = textEdit1.Text.Trim(),
                WarehouseId = (long)lookUpEdit1.EditValue,
                WarehouseAreaAddress = textEdit3.Text.Trim(),
                Area = textEdit5.Text.Trim(),
                Principal= textEdit4.Text.Trim(),
                Remark = memoEdit1.Text.Trim()
            };

            var response = await PublicFields.client.PostAsJsonAsync("http://localhost:5113/api/WarehouseArea/AddWarehouseArea", model);
            if (response.IsSuccessStatusCode)
            {
                // 调用委托，通知父窗体刷新
                WarehouseAdded?.Invoke();
                this.Close();
            }
            else
            {
                MessageBox.Show("添加失败：" + response.StatusCode);
            }
        }
        public class WarehouseAreaModel
        {
            public string WarehouseAreaCode { get; set; }
            public string WarehouseAreaName { get; set; }
            public long WarehouseId { get; set; }
            public string WarehouseAreaAddress { get; set; }
            public string Area { get; set; }
            public string Principal { get; set; }
            public string Remark { get; set; }
        }
    }
}